<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Http\Middleware;

use App\Models\System\Config;
use App\Models\System\Email;
use App\Models\System\Info;
use App\Models\System\Permission;
use App\User;
use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;

class Sidebar
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure                 $next
     *
     * @return mixed
     */
    public function handle( $request, Closure $next )
    {
        $this->icon();//获取配置
        $this->menus();//获取权限列表
        $this->bible();//获取侧标栏随机字符
        return $next($request);
    }

    /**
     * 获取配置
     */
    public function icon()
    {
        $select = [
            'name', 'url', 'title', 'word', 'riptive', 'record', 'info', 'company', 'qq', 'box', 'phone', 'fixed',
            'fax', 'image',
        ];
        $with   = [
            'file' => function ( $query ) {
                $query->select('id', 'url', 'file_url');
            },
        ];
        $config = Config::FindModel([ 'status' => 1 ], $with, $select);//SEO信息
        $email  = Email::FindModel([ 'smtp', 'server', 'port', 'sender', 'name', 'username', 'password' ]);//邮箱信息
        unset($with);
        $with = [
            'info' => function ( $query ) {
                $query->select('avatar', 'uid', 'content', 'username', 'rename', 'editor');
                $query->with([
                    'file' => function ( $query ) {
                        $query->select('id', 'url', 'file_url');
                    },
                ]);
            },
        ];
        $user = User::FindModel(Auth::id(), $with, [ 'name', 'id', 'email' ]);//用户信息
        $fid  = Permission::FirstModel([ 'label' => 'system.modify.index' ], [], [ 'id', 'name', 'label' ]);//查询有没有这个菜单
        if ( empty($fid) ) {
            $fid[ 'id' ]    = 6;
            $fid[ 'name' ]  = '个人设置';
            $fid[ 'label' ] = 'system.modify.index';
        }//默认给设置
        view()->share(compact('config', 'email', 'user', 'fid'));
    }

    /**
     * 获取权限菜单
     */
    public function menus()
    {
        $select      = [ 'sort_order', 'id', 'created_at', 'parent_id', 'icon', 'label', 'name', 'pid' ];
        $permissions = Permission::get_children($select);
        //三元判断如果是超级管理员 就 获得所有权限 否则 就获得有限的权限，通过get_menus()方法定义
        $menus = Auth::user()->hasRole('超级管理员') ? $permissions : $this->get_menus($permissions);
        view()->share('menus', $menus);
    }

    /**
     * @param $permissions
     *私有方法 获取属于自己得菜单
     *
     * @return mixed
     */
    private function get_menus( $permissions )
    {
        foreach ( $permissions as $key => $permission ) {
            foreach ( $permission->children as $k => $children ) if ( Gate::denies($children->label) ) unset($permissions[ $key ][ 'children' ][ $k ]);
            if ( $permission->children->isEmpty() ) unset($permissions[ $key ]);
        }
        return $permissions;
    }

    /**
     * 侧边栏
     */
    private function bible()
    {
        @$bibles = file('bible.txt');//将文件存入一个数组
        $size  = count($bibles) / 2 - 1;//计算数组中的单元数组，或对象中的属性个数
        $rand  = rand(0, $size) * 2;//产生一个随机数
        $bible = [
            'cn' => $bibles[ $rand + 1 ], 'en' => $bibles[ $rand ],
        ];//新建一个数组
        view()->share('bible', $bible);
    }
}
